//
//  AKMiniblogEntry.m
//  CocoaBean
//
//  Created by sleeper on 4/19/10.
//  Copyright 2010 __MyCompany__ All rights reserved.
//

#import "NSDate_DoubanAddition.h"
#import "NSDictionary_DoubanAddition.h"

#import "AKEntry.h"
#import "AKEntry_Private.h"

#import "AKMiniblogEntry.h"

NSString * const _kAKMiniblogEntryEncodeKeyDate = @"akDate";

@interface AKMiniblogEntry ()
@property (nonatomic, assign) NSUInteger commentsCount;
@end

@implementation AKMiniblogEntry

@synthesize commentsCount = _commentsCount;

- (id)initWithDictionary:(NSDictionary *)dictionary
{
	if ((self = [super initWithDictionary:dictionary])) {
		_commentsCount = NSNotFound;
	}
	return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
	if ((self = [super initWithCoder:aDecoder])) {
		_publishDate = [[aDecoder decodeObjectForKey:_kAKMiniblogEntryEncodeKeyDate] copy];
	}
	return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
	[super encodeWithCoder:aCoder];
	[aCoder encodeObject:self.publishDate forKey:_kAKMiniblogEntryEncodeKeyDate];
}

- (NSComparisonResult)compare:(AKMiniblogEntry *)anotherEntry
{
	return [self.publishDate compare:anotherEntry.publishDate];
}

- (void)dealloc
{
	AK_RELEASE(_akReserved);
	AK_RELEASE(_publishDate);
	AK_RELEASE(_publishDateString);
	[super dealloc];
}

#pragma mark Author

- (NSString*)authorName
{
	return [self.dictionary akPropertyForKey:kAKEntryKeyAuthor subkey:kAKEntryKeyName valueKey:nil];
}

- (NSString*)authorIconURL
{
	return [self.dictionary akPropertyInArrayForKey:kAKEntryKeyAuthor 
										firstSubkey:kAKEntryKeyLink
									   secondSubkey:kAKEntrySubkeyRel 
										   valueKey:kAKEntrySubkeyHref
											  value:@"icon"];
}

- (NSString*)authorAlternateAddress
{
	return [self.dictionary akPropertyInArrayForKey:kAKEntryKeyAuthor
										firstSubkey:kAKEntryKeyLink
									   secondSubkey:kAKEntrySubkeyRel 
										   valueKey:kAKEntrySubkeyHref
											  value:@"alternate"];
}

- (NSString*)authorURI
{
	return [self.dictionary akPropertyForKey:kAKEntryKeyAuthor subkey:kAKEntryKeyURI valueKey:nil];
}

- (NSUInteger)commentsCount
{
	if (_commentsCount == NSNotFound) {
		id countValue = [self.dictionary akPropertyInArrayForKey:kAKEntryKeyDoubanAttribute subkey:kAKEntrySubkeyName valueKey:nil value:@"comments_count"];
		if (countValue != nil) {
			_commentsCount = [countValue integerValue];
//			if ([countValue isKindOfClass:[NSNumber class]])
//				_commentsCount = [countValue unsignedIntValue];
//			else if ([countValue isKindOfClass:[NSString class]])
//				_commentsCount = [countValue integerValue];
		}
		
		if (_commentsCount == NSNotFound)
			_commentsCount = 0;
	}
	
	return _commentsCount;
}

#pragma mark Category

- (NSString*)subcategoryString
{
	return [self.dictionary akPropertyInArrayForKey:kAKEntryKeyDoubanAttribute subkey:kAKEntryKeyName valueKey:nil value:@"category"];
}

#pragma mark Content

- (NSString*)content
{
	return [self.dictionary akPropertyForKey:kAKEntryKeyContent valueKey:nil];
}

#pragma mark Date

- (NSString *)publishDateString
{
	if (_publishDateString == nil)
		_publishDateString = [[self.dictionary akPropertyForKey:kAKEntryKeyPublished valueKey:nil] copy];
	return _publishDateString;
}

- (NSDate *)publishDate
{
	if (_publishDate == nil)
		_publishDate = [[NSDate dbDateForString:self.publishDateString] copy];
	return _publishDate;
}

//#pragma mark Title
//
//- (NSString*)title
//{
//	return [self.dictionary akPropertyForKey:kAKEntryTitle valueKey:nil];
//}

@end
